<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
  >
    <el-form
        ref="dataForm"
        :model="dataForm"
        :rules="dataRules"
        label-width="100px"
        @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
      </el-form-item>
      <el-form-item label="IP地址" prop="ipAddress">
        <el-input v-model="dataForm.ipAddress" placeholder="IP地址"></el-input>
      </el-form-item>
      <el-form-item label="登录时间" prop="loginTime">
        <el-date-picker
            v-model="dataForm.loginTime"
            type="datetime"
            placeholder="选择登录时间"
            value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="登出时间" prop="logoutTime">
        <el-date-picker
            v-model="dataForm.logoutTime"
            type="datetime"
            placeholder="选择登出时间"
            value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// 数字，邮件，手机，url，身份证校验
import { isNumber,isIntNumer,isEmail,isPhone, isMobile,isURL,checkIdCard } from "@/utils/validate";

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        username: '',
        userId: '',
        ipAddress: '',
        loginTime: '',
        logoutTime: ''
      },
      dataRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' }
        ],
        ipAddress: [
          { required: true, message: 'IP地址不能为空', trigger: 'blur' }
        ],
        loginTime: [
          { required: true, message: '登录时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getLogInfo(this.dataForm.id).then(response => {
            this.dataForm = response.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const request = this.dataForm.id
              ? updateLog(this.dataForm)
              : addLog(this.dataForm)
          request.then(() => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>